<?php include('header.php')?>
<div style="padding-left: 50px;">
<form id="formid" method="post" action="">
<?php
include('connect.php');
$client_id = '893977637530-jcdqd8sp88drksajq8famftsguqi2087.apps.googleusercontent.com';
$client_secret = 'ux50pAQyZQqYQtFf6p4Nj-y-';
$redirect_uri = 'http://localhost/newsletteraapthi/oauth.php';
$max_results = 100;

$auth_code = $_GET["code"];

function curl_file_get_contents($url)
{
 $curl = curl_init();
 $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
 
 curl_setopt($curl,CURLOPT_URL,$url);	//The URL to fetch. This can also be set when initializing a session with curl_init().
 curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);	//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
 curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);	//The number of seconds to wait while trying to connect.	
 
 curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);	//The contents of the "User-Agent: " header to be used in a HTTP request.
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);	//To follow any "Location: " header that the server sends as part of the HTTP header.
 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);	//To automatically set the Referer: field in requests where it follows a Location: redirect.
 curl_setopt($curl, CURLOPT_TIMEOUT, 10);	//The maximum number of seconds to allow cURL functions to execute.
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);	//To stop cURL from verifying the peer's certificate.
 
 
 $contents = curl_exec($curl);
 curl_close($curl);
 return $contents;
}

$fields=array(
    'code'=>  urlencode($auth_code),
    'client_id'=>  urlencode($client_id),
    'client_secret'=>  urlencode($client_secret),
    'redirect_uri'=>  urlencode($redirect_uri),
    'grant_type'=>  urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');

//echo $post; exit;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($curl);
curl_close($curl);

$response =  json_decode($result);
if(isset($response->access_token)){
$accesstoken = $response->access_token;
}else{
$accesstoken ="";
}
//$accesstoken = $response->access_token;

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);
echo "<h3>Email Addresses:</h3>";
?>
<center><table border=1px;>
<thead><tr><th><input type="checkbox" id="selectAll" onclick="checkAll();"></th><th>Email</th></tr></thead>
<?php
$xml =  new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
foreach ($result as $title) {
  $email=$title->attributes()->address;
  ?>
  <tbody>
	<tr><td><input type="checkbox" id="checkbox[]" name="checkbox[]" onclick="checkVal();" value="<?php echo $email;?>"></td><td><?php echo $email;?></td></tr>
<?
}
?>
</tbody>
</table></center>
<?php
$resultSet="";
	if(isset($_POST['checkbox'])){
			foreach($_POST['checkbox'] as $checkboxvalue){
				$sql5="select * from friends where email_id='".$checkboxvalue."'";
				$ress=mysql_query($sql5,$con);
				$result=mysql_num_rows($ress);
				if($result=='0'){
					$sql4=mysql_query("INSERT INTO friends(email_id,added_date)
					VALUES('".$checkboxvalue."',NOW())");
					$resultSet=$sql4;
				}
			}
			if($resultSet==''){?>
			<script>alert('Already Inserted');window.location='http://localhost/newsletteraapthi/gmail.php';</script>
		<?}
		if($resultSet>0){?>
			<script>alert('Sent successfully.');window.location='http://localhost/newsletteraapthi/gmail.php';</script>
		<?}
	}
	?>
             <div style="text-align:center;">
                 <a  class="news_btn"  href="#" id="sendEmail"  onclick="return sendMailCheck();">Save</a>
			</div>
 </form>
</div>
<?php include('footer.php')?> 
<script src="js/jquery.js"></script>
<script>
	function checkAll(){
			if (document.getElementById('selectAll').checked) {
				for(var a=0; a<document.getElementsByTagName('input').length;a++){
				  document.getElementsByTagName('input')[a].checked=true;
				}
			}
			else {
				for( var j=0; j<document.getElementsByTagName('input').length;j++){
				   document.getElementsByTagName('input')[j].checked=false;
				}
			}
	}
	function checkVal(){ 
		var checkval=document.getElementsByName('checkbox[]');         
		var len; var count=0;
		len=checkval.length;
		for(var i=0;i<len;i++){ 
			if(checkval[i].checked){
				count++;
			}
		}
		if(len==count){
			document.getElementById('selectAll').checked=true;  
		}
		else{
			document.getElementById('selectAll').checked=false;
		}
	}
	function sendMailCheck()
	{
		var flag=true;
		if($("input[name='checkbox[]']:checked").length<=0) {
			alert("check The  sendedMail checkbox");
			return false;
		}
		if(flag==false){
		 return false;
		}else{
		  $("#formid").submit();
		}
		
	}
</script>